package com.unisv.dj;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * 
 * @author fanzhen
 * @time 8:11:45 PM Aug 19, 2011
 * @email fanzhen.meng@gmail.com
 */
public class Main extends Activity {
	private static final String TAG = Main.class.getSimpleName();
	String ip = "192.168.0.188";
	int port = 8080;
	ImageButton delButton;
	ImageButton plusButton;
	ImageButton cmdButton;
	SharedPreferences speed;
	Editor speedRecord;
	TextView speedText;
	byte[] command;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		speed = getSharedPreferences(TAG, 40);
		speedRecord = speed.edit();
		speedRecord.putInt(TAG, 40);
		speedRecord.commit();
		speedText = (TextView) findViewById(R.id.speed);
		speedText.setText("40");
		delButton = (ImageButton) findViewById(R.id.imageButton1);
		plusButton = (ImageButton) findViewById(R.id.imageButton2);
		cmdButton = (ImageButton) findViewById(R.id.imageButton3);
		command = new byte[4];
		delButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				int current = speed.getInt(TAG, 40);
				if (current - 20 >= 20) {
					current = current - 20;
					speedText.setText(String.valueOf(current));
					speedRecord.putInt(TAG, current);
					speedRecord.commit();
				}
				doAction();
			}
		});
		plusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				int current = speed.getInt(TAG, 40);
				if (current + 20 <= 60) {
					current = current + 20;
					speedText.setText(String.valueOf(current));
					speedRecord.putInt(TAG, current);
					speedRecord.commit();
				}
				doAction();
			}
		});
		cmdButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Runnable runnable = new Runnable() {
					@Override
					public void run() {
						command[0] = 1;
						if (speed.getInt(TAG, 40) == 20) {
							command[1] = 0;
						} else if (speed.getInt(TAG, 40) == 60) {
							command[1] = 2;
						} else {
							command[1] = 1;
						}
						if (command[2] == 0)
							command[2] = 1;
						else
							command[2] = 0;
						command[3] = 0;
						// Log.i(TAG,
						// String.valueOf(command[0])+String.valueOf(command[1])+String.valueOf(command[2]));
						try {
							Socket socket = new Socket(ip, port);
							OutputStream out = socket.getOutputStream();
							out.write(command);
							out.close();
							socket.close();
						} catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				};
				new Thread(runnable).start();
			}
		});
	}

	public void doAction() {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				command[0] = 1;
				if (speed.getInt(TAG, 40) == 20) {
					command[1] = 0;
				} else if (speed.getInt(TAG, 40) == 60) {
					command[1] = 2;
				} else {
					command[1] = 1;
				}
				command[2] = 1;
				command[3] = 0;
				// Log.i(TAG,
				// String.valueOf(command[0])+String.valueOf(command[1])+String.valueOf(command[2]));
				try {
					Socket socket = new Socket(ip, port);
					OutputStream out = socket.getOutputStream();
					out.write(command);
					out.close();
					socket.close();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		new Thread(runnable).start();
	}
}